#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "Service.h"
#include "Private.h"
#include <unistd.h>
#include <fcntl.h>

/// <summary>
/// Locals
/// </summary>
siginfo_t info;
sigset_t waitset;
struct sigaction sigact;

/// <summary>
/// TODO
/// </summary>
int SignalsInit()
{
	sigemptyset(&waitset);
	sigaddset(&waitset, SIGINT);
	sigaddset(&waitset, SIGPWR);
	sigaddset(&waitset, SIGHUP);
	sigaddset(&waitset, SIGABRT);
	sigaddset(&waitset, SIGALRM);
	sigaddset(&waitset, SIGTERM);
	sigaddset(&waitset, SIGQUIT);
	sigaddset(&waitset, SIGCHLD);
	sigaddset(&waitset, SIGILL);
	sigaddset(&waitset, SIGFPE);
	sigaddset(&waitset, SIGSEGV);
	sigaddset(&waitset, SIGBUS);

	if (sigprocmask(SIG_BLOCK, &waitset, NULL) < 0)
		return -1;

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int SignalsWait(void)
{
	int sig = 0;
	if ((sig = sigwaitinfo(&waitset, &info)) == -1)
		return -1;
	HandleSignals(info);
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int HandleSignals(siginfo_t info)
{
	printf("Signal\n");
	printf("\tNumber: %d\n", info.si_signo);
	printf("\tError: %d\n", info.si_errno);
	printf("\tValue: %d\n", info.si_value.sival_int);
	printf("\tExit: %d\n", info.si_status);
	printf("\tCode: %d\n", info.si_code);
	printf("\tPID: %06d\n", info.si_pid);
	printf("\tUID: %06d\n", info.si_uid);
	return 0;
}
